package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description:Jboss servlet各项指标的采集 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:53:32          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ServletCollector extends Collector {
    private static Logger logger = Logger.getLogger(ServletCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        //        String getManagerName = this.getParamMap().get("名称").trim();
        String getManagerName = this.getParamMap().get("Name").trim();
        String servletName = null;
        boolean statisticsProvider = true;
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            Set<ObjectName> objectNames = RmiUtil.queryObjectNames(rmiAdapter, "jboss.web:j2eeType=Servlet,*");
            for (ObjectName jbossServlet : objectNames) {
                servletName = (String) rmiAdapter.getAttribute(jbossServlet, "objectName");
                if (getManagerName.equals(servletName)) {
                    statisticsProvider = (Boolean) rmiAdapter.getAttribute(jbossServlet, "statisticsProvider");
                    String loadTime = rmiAdapter.getAttribute(jbossServlet, "loadTime") + "";
                    String classLoadTime = rmiAdapter.getAttribute(jbossServlet, "classLoadTime") + "";
                    String processingTime = rmiAdapter.getAttribute(jbossServlet, "processingTime") + "";
                    String requestCount = rmiAdapter.getAttribute(jbossServlet, "requestCount") + "";
                    String maxTime = rmiAdapter.getAttribute(jbossServlet, "maxTime") + "";

                    propValues[0] = ResPropertyValue.createPropValue(statisticsProvider ? "YES" : "NO", propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(loadTime, propMetas[1]);
                    propValues[2] = ResPropertyValue.createPropValue(classLoadTime, propMetas[2]);
                    propValues[3] = ResPropertyValue.createPropValue(processingTime, propMetas[3]);
                    propValues[4] = ResPropertyValue.createPropValue(requestCount, propMetas[4]);
                    propValues[5] = ResPropertyValue.createPropValue(maxTime, propMetas[5]);

                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                    return values;
                }
            }
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "名称" };
        return new String[] { "Port", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "是否支持性能统计", "加载次数", "类加载次数", "请求总处理时间", "请求数量", "最大处理时长" };
        //        return new String[] { "SupportPerformanceStatisticOrNot", "LoadTimes", "ClassLoadTimes", "TotalRequestHandleTime", "RequestsNum", "MaxHandleTime" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }

}
